package server.consolo;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import qq.message.DataPackage;
import qq.message.DataPackage.MessageType;

public class ServerTest {

	private JFrame mainFrame;
	   private JLabel headerLabel;
	   private JLabel statusLabel;
	   private JPanel controlPanel;

	   public ServerTest(){
	      prepareGUI();
	   }


	   private void prepareGUI(){
	      mainFrame = new JFrame("Java Swing Examples");
	      mainFrame.setSize(400,400);
	      mainFrame.setLayout(new GridLayout(3, 1));
	      mainFrame.addWindowListener(new WindowAdapter() {
	         public void windowClosing(WindowEvent windowEvent){
	            System.exit(0);
	         }        
	      });    
	      headerLabel = new JLabel("", JLabel.CENTER);        
	      statusLabel = new JLabel("",JLabel.CENTER);   
	      headerLabel.setText("Control in action: JTextArea");

	      statusLabel.setSize(350,100);

	      controlPanel = new JPanel();
	      controlPanel.setLayout(new FlowLayout());

	      mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//点击x关闭
	      mainFrame.add(headerLabel);
	      mainFrame.add(controlPanel);
	      mainFrame.add(statusLabel);
	      mainFrame.setVisible(true);  
	   }

	   private void showTextAreaDemo(){
		   
	      JLabel  commentlabel= new JLabel("Comments: ", JLabel.RIGHT);
	      final JTextArea commentTextArea = 
	         new JTextArea("",5,20);
	      JScrollPane scrollPane = new JScrollPane(commentTextArea);    

	      JButton showButton = new JButton("Show");

	      showButton.addActionListener(new ActionListener() {
	         public void actionPerformed(ActionEvent e) {     
	        	 try {
	        		 statusLabel.setText( commentTextArea.getText());  
	        		 Socket socket=new Socket("127.0.0.1",8888);
	        			OutputStream os=socket.getOutputStream();
	        			System.out.println("add client 1");
	        			InputStream is=socket.getInputStream();
	        			System.out.println("add client 2");
	        			ObjectOutputStream oos=new ObjectOutputStream(os);
	        			System.out.println("add client 3");
	        			ObjectInputStream ois = new ObjectInputStream(is);
	        			System.out.println("add client 4");
	        			DataPackage data=new DataPackage();
	        			data.setFromName("zhuyi");
	        			data.setToName("zhuyi");
	        			data.setMessageType(MessageType.LOGIN);
	        			data.setMessageData("");
	        			data.setTime(System.currentTimeMillis());
	        			System.out.println(data.toString());
	        			oos.writeObject(data);
	        			System.out.println("add client ");
	        			
	        			System.out.println();
	        			while(true)
	        			{
	        				System.out.println("alive");
	        				if(ois.available()>10)
	        				{
	        					data=(DataPackage) ois.readObject();
	        					System.out.println(data.toString());
	        					break;
	        				}
	        				
	        			}
	        			oos.close();
	        			ois.close();
				} catch (UnknownHostException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	         }
	      }); 

	      controlPanel.add(commentlabel);
	      controlPanel.add(scrollPane);        
	      controlPanel.add(showButton);
	      mainFrame.setVisible(true);  
	   }
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ServerTest  serverTest = new ServerTest();      
		serverTest.showTextAreaDemo();
	}

}
